package factorytest.iflytek.com.einkiflytekfactorytest.factorytestui;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.android.server.roobo.NVRomIntegerInfo;
import com.android.server.roobo.NVRomStringInfo;
import com.android.server.roobo.RobotNVRomDataLocalJni;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import factorytest.iflytek.com.einkiflytekfactorytest.R;
import factorytest.iflytek.com.einkiflytekfactorytest.utils.FileReadWrite;

public class NvromTestActivity extends FactoryBaseActivity implements View.OnClickListener {
    private TextView txtLog;

    private EditText editIntKey;
    private EditText editIntValue;
    private EditText editStringKey;
    private EditText editStringValue;

    private Button btnWriteInt;
    private Button btnWriteString;
    private Button btnReadAll;

    @Override
    public int getLayoutId() {
        return R.layout.activity_nvrom_test;
    }

    @Override
    public void onInitView(@Nullable Bundle savedInstanceState) {
        txtLog = (TextView) findViewById(R.id.txtLog);

        findViewById(R.id.btnWriteInt).setOnClickListener(this);
        findViewById(R.id.btnWriteString).setOnClickListener(this);
        findViewById(R.id.btnReadAll).setOnClickListener(this);
        findViewById(R.id.btnClearLog).setOnClickListener(this);


        editIntKey = findViewById(R.id.editIntKey);
        editIntValue = findViewById(R.id.editIntValue);
        editStringKey = findViewById(R.id.editStringKey);
        editStringValue = findViewById(R.id.editStringValue);

    }

    private final static int MSG_UPDATE_LOG = 100;

    @Override
    public void onHandleMessage(Handler handler, Message msg) {

        switch (msg.what) {
            case MSG_UPDATE_LOG:
                doUpdateLog((String) msg.obj);
                break;
            default:
                break;
        }
    }

    private void updateLog(String log) {
        getHandler().obtainMessage(MSG_UPDATE_LOG, log).sendToTarget();
    }

    private void doUpdateLog(String log) {
        String txt = txtLog.getText().toString();
        if (txt.length() > 1024 * 6) {
            txtLog.setText(log);
        } else {
            txtLog.setText(txt + "\n" + log);
        }
    }


    @Override
    public void onClick(View v) {
        RobotNVRomDataLocalJni nvrom = RobotNVRomDataLocalJni.getInstance();
        switch (v.getId()) {
            case R.id.btnWriteInt:
                String keyInt = editIntKey.getText().toString();
                int keyValue = Integer.parseInt(editIntValue.getText().toString());
                nvrom.setIntegerNVRom(keyInt, keyValue);
                int retInt = nvrom.getIntegerNVRom(keyInt, 0);
                updateLog("read-> key = " + keyInt + " : value = " + retInt);
                break;
            case R.id.btnWriteString:
                String keyString = editStringKey.getText().toString();
                String keyStringValue = editStringValue.getText().toString();
                nvrom.setStringNVRom(keyString, keyStringValue);
                String retString = nvrom.getStringNVRom(keyString);
                updateLog("read-> key = " + keyString + " : value = " + retString);
                break;
            case R.id.btnReadAll:


                int countInt = nvrom.getCountOfAllIntegerNVRom();

                NVRomIntegerInfo[] intInfos = nvrom.getAllIntegerNVRom(0, countInt);

                updateLog("key-integer count is " + countInt + ", all keys is following:");

                for (NVRomIntegerInfo info : intInfos) {
                    updateLog(info.toString());

                }

                int countString = nvrom.getCountOfAllStringNVRom();

                NVRomStringInfo[] strInfos = nvrom.getAllStringNVRom(0, countString);

//                updateLog("key-string count is " + countString + ", all keys is following:");

                List<NVRomStringInfo> list = new ArrayList<>(Arrays.asList(strInfos));

                Collections.sort(list, new Comparator<NVRomStringInfo>() {
                    @Override
                    public int compare(NVRomStringInfo o1, NVRomStringInfo o2) {
                        return o1.toString().compareTo(o2.toString());
                    }
                });

                for (NVRomStringInfo info : list) {
                    if (info.key.contains("afterAging")) {
                        nvrom.removeStringNVRom(info.key);
                    } else if (info.key.contains("aging")) {
                        nvrom.removeStringNVRom(info.key);
                    }

                }

                updateString();

                break;
            case R.id.btnClearLog:
                txtLog.setText("");
                break;
        }
    }

    private void updateString() {
        RobotNVRomDataLocalJni nvrom = RobotNVRomDataLocalJni.getInstance();

        int countString = nvrom.getCountOfAllStringNVRom();

        NVRomStringInfo[] strInfos = nvrom.getAllStringNVRom(0, countString);

        updateLog("key-string count is " + countString + ", all keys is following:");

        List<NVRomStringInfo> list = new ArrayList<>(Arrays.asList(strInfos));

        Collections.sort(list, new Comparator<NVRomStringInfo>() {
            @Override
            public int compare(NVRomStringInfo o1, NVRomStringInfo o2) {
                return o1.toString().compareTo(o2.toString());
            }
        });

        for (NVRomStringInfo info : list) {
            updateLog(info.toString());
        }
    }

}
